//在每个树行中找最大值
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        if (root == nullptr)
            return {};
        vector<int> ret;
        queue<TreeNode*> q;
        q.push(root);
        while (q.size()) {
            int n = q.size();
            int M = (q.front())->val;
            for (int i = 0; i < n; i++) {
                TreeNode* t = q.front();
                q.pop();
                M = max(M, t->val);
                if (t->left)
                    q.push(t->left);
                if (t->right)
                    q.push(t->right);
            }
            ret.push_back(M);
        }
        return ret;
    }
};